/********************************************************************************************
** Copyright � 2011 Nokia Corporation. All rights reserved.
** Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation.
** Java and all Java-based marks are trademarks or registered trademarks of
** Sun Microsystems, Inc. Other product and company names mentioned herein may be
** trademarks or trade names of their respective owners.
**
** Subject to the conditions below, you may, without charge:
**
** �  Use, copy, modify and/or merge copies of this software and
**    associated content and documentation files (the �Software�)
**
** �  Publish, distribute, sub-licence and/or sell new software
**    derived from or incorporating the Software.
**
** Some of the documentation, content and/or software maybe licensed under open source
** software or other licenses.  To the extent such documentation, content and/or
** software are included, licenses and/or other terms and conditions shall apply
** in addition and/or instead of this notice. The exact terms of the licenses, disclaimers,
** acknowledgements and notices are reproduced in the materials provided.
**
** This file, unmodified, shall be included with all copies or substantial portions
** of the Software that are distributed in source code form.
**
** The Software cannot constitute the primary value of any new software derived
** from or incorporating the Software.
**
** Any person dealing with the Software shall not misrepresent the source of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
** INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
** PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
** HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
********************************************************************************************/
import Qt 4.7

import "DCDataClasses.js" as DCDATA

Rectangle {
    id: dialog
    width: 550
    height: 400
    z: 2
    color: "#5d8eac"
    gradient: Gradient {
        GradientStop {
            position: 0.00;
            color: dialog.color;
        }
        GradientStop {
            position: 1.00;
            color: dialog.color;
        }
    }
    transformOrigin: Item.Center
    border.width: 0
    border.color: "#000000"

    property Messages messages: null

    property alias label: titletext.text
    property alias titlerect: titlerectdlg

    property bool unreadmessages: false



    signal ok
    signal cancel

    signal menuSelection(string selectionlabel)

    function newmessageavailablecb(newmessages)
    {
        unreadmessages = newmessages
    }

    Component.onCompleted:
    {
        DCDATA.appendmessagecb(newmessageavailablecb)
    }

    function setGPSOnIconVisible(visibility)
    {
        gpsonrect.visible = visibility
    }

    // menu
    function addMenuItems(menuitemarray,okandcancel)
    {
        var menuitemstr = ""
        var i = 0
        //menuitemModel.clear()
        while (menuitemModel.count > 0)
        {
            menuitemModel.remove(0)
        }

        for (i=0; i<menuitemarray.length;i++)
        {
            menuitemModel.append({"name":menuitemarray[i]})
        }

        if (okandcancel)
        {
            // the default items are the last in the menu
            menuitemModel.append({"name":"OK"})
            menuitemModel.append({"name":"Cancel"})

        }

        menuitemModel.append({"name":"Exit"})

    }


    ListModel {
        id: menuitemModel

        ListElement {name: "OK"}
        ListElement {name: "Cancel"}
        ListElement {name: "Exit"}
    }

    property int menufontpixelsize: 22
    property int menuwidth: 220


    Rectangle {
        id: menuselectionrect
        width: menuwidth
        height: (menufontpixelsize + 10) * menuitemModel.count
        anchors { top: titlerectdlg.bottom }
        color: "lightyellow"
        border.color: "#ffffff"
        anchors.right: parent.right
        anchors.rightMargin: 0
        anchors.topMargin: 0

        scale: 0.0
        opacity: 0.0

        Column
        {
            spacing: 0
            anchors.right: parent.right
            anchors.rightMargin: 0
            anchors.left: parent.left
            anchors.leftMargin: 0
            Repeater {
                id: repeatermenu
                model: menuitemModel.count


                Rectangle {
                    id: menuitemrect
                    height: menufontpixelsize + 10
                    width:  menuwidth
                    color: "lightyellow"
                    //anchors.right: parent.right
                    //anchors.rightMargin: 0
                    //anchors.left: parent.left
                    //anchors.leftMargin: 0
                    border.color: "#000000"
                    Text {
                        id: menustr
                        text: menuitemModel.get(index).name
                        font.family: "Arial";
                        font.pixelSize: menufontpixelsize
                        anchors.centerIn: menuitemrect

                    }

                    MouseArea {
                        anchors.fill: menuitemrect

                        onEntered:
                        {
                            parent.color = "yellow"
                        }
                        onExited:
                        {
                            parent.color = "lightyellow"
                        }
                        onClicked:
                        {
                            menuselectionrect.visible = false
                            if (menuitemModel.get(index).name == "OK")
                            {
                                ok()
                            }
                            else if (menuitemModel.get(index).name == "Cancel")
                            {
                                cancel()
                            }
                            else if (menuitemModel.get(index).name == "Exit")
                            {
                                Qt.quit()
                            }
                            else
                            {
                                menuSelection(menuitemModel.get(index).name)
                            }
                        }

                    }
                }
            }
        }



        visible: false
    }

    // --------

    Rectangle {
        id: titlerectdlg
        height: 40
        transformOrigin: Item.TopLeft
        border.width: 0
        gradient: Gradient {
            GradientStop {
                position: 0.46
                color: "#87cefa"
            }

            GradientStop {
                position: 1
                color: "#000000"
            }
        }
        border.color: "#000000"
        anchors.right: parent.right
        anchors.rightMargin: 0
        anchors.left: parent.left
        anchors.leftMargin: 0
        anchors.top: parent.top
        anchors.topMargin: 0



        Text {
            id: titletext
            color: "#00008b"
            text: "Title"
            anchors.right: menurect.left
            anchors.rightMargin: 0
            //style: Text.Sunken
            anchors.left: parent.left
            anchors.leftMargin: 10
            anchors.verticalCenter: titlerectdlg.verticalCenter
            font.family: "Arial"
            font.pixelSize: 24
        }

        Rectangle {
            id: menurect
            width: 60
            transformOrigin: Item.TopLeft
            border.width: 0
            color: "#00000000"

            anchors.bottom: parent.bottom
            anchors.bottomMargin: 0
            anchors.top: parent.top
            anchors.topMargin: 0
            anchors.right: parent.right
            anchors.rightMargin: 0


            Image {
                id: menuimg
                anchors.fill: parent
                width: 60
                height: 40
                fillMode: Image.PreserveAspectFit
                source: "menuimg.gif"
                opacity: 1.0

                MouseArea {
                    id: menumousearea
                    anchors.fill: parent
                    onPressed:
                    {
                        menurect.color = "#397191"
                    }

                    onReleased:
                    {
                        menurect.color = "#00000000"
                    }
                    onClicked: {
                        if (menuselectionrect.visible)
                        {
                            menuselectionrect.visible = false
                        }
                        else
                        {
                            menuselectionrect.z = 99
                            menuselectionrect.visible = true
                        }
                    }
                }
            }
        }

        Rectangle {
            id: messagerect
            width: 40
            transformOrigin: Item.TopLeft
            border.width: 0
            color: "#00000000"

            anchors.right: menurect.left
            anchors.rightMargin: 0
            anchors.bottom: parent.bottom
            anchors.bottomMargin: 0
            anchors.top: parent.top
            anchors.topMargin: 0

            Image {
                id: messageimg
                anchors.fill: parent
                width: 40
                height: 38
                fillMode: Image.Stretch
                source: (dialog.unreadmessages?"newmessage.png":"message.png")
                opacity: 1.0

                MouseArea {
                    id: messagemousearea
                    anchors.fill: parent
                    onPressed:
                    {
                        messagerect.color = "#397191"
                    }

                    onReleased:
                    {
                        messagerect.color = "#00000000"
                    }

                    onClicked:
                    {
                        messages.show()
                    }
                }
            }
        }

        Rectangle {
            id: gpsonrect
            width: 20
            height: 20
            transformOrigin: Item.TopLeft
            border.width: 0
            color: "#00000000"
            visible:  false

            anchors.right: messagerect.left
            anchors.rightMargin: 0
            anchors.bottom: parent.bottom
            anchors.bottomMargin: 0
            anchors.top: parent.top
            anchors.topMargin: 0

            Image {
                id: gpsonimg
                anchors.fill: parent
                width: 20
                height: 20
                fillMode: Image.PreserveAspectFit
                source: "gpson.png"
                opacity: 1.0

            }
        }
    }

    states:
    [
        State {
            name: "openmenu"; when: menuselectionrect.visible == true
            PropertyChanges { target: menuselectionrect; scale: 1.0; opacity: 1.0; rotation: 360 }

        }
    ]

    transitions:
    [
        Transition
        {
            from: ""; to: "openmenu"; reversible: true
            ParallelAnimation {
                NumberAnimation { properties: "scale"; duration: 610; easing.type: Easing.Linear }
                NumberAnimation { properties: "opacity"; duration: 610; easing.type: Easing.Linear }
                NumberAnimation { properties: "rotation"; duration: 610; easing.type: Easing.Linear }

            }
        }
    ]



}
